//! ## 7. Reverse Integer (easy)
//! Given a 32-bit signed integer, reverse digits of an integer.
//! For this problem, assume that your function returns 0 when the reversed integer overflows.
//! ### 知识点
//! - 迭代器
//!     - chars(): 将 String 转为字符迭代器
//!     - rev(): 将迭代器内元素反转
//!     - collect(): 消费迭代器, 将其转为容器
//! - parse(): 将字符串转为其他类型
//! - i32
//!     - signum(): 获取符号(正数 1, 0, 负数 -1)
//!     - abs(): 获取绝对值

pub struct Solution;

impl Solution {
    pub fn solve(x: i32) -> i32 {
        let x_str = x.abs().to_string().chars().rev().collect::<String>();
        match x_str.parse::<i32>() {
            Ok(x_) => x.signum() * x_,
            _ => 0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let x = 2_147_483_647;
        let res = 0;
        assert_eq!(Solution::solve(x), res);
        let x = 123_456_789;
        let res = 987_654_321;
        assert_eq!(Solution::solve(x), res);
        let x = -123;
        let res = -321;
        assert_eq!(Solution::solve(x), res);
    }
}
